package com.sita.linboard.person;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.sita.linboard.R;
import com.sita.linboard.base.BaseActivity;
import com.sita.linboard.base.BaseApplication;
import com.sita.linboard.utils.SpUtils;
import com.sita.linboard.utils.ToastUtils;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.shareboard.SnsPlatform;
import com.umeng.socialize.utils.Log;
import com.umeng.socialize.utils.ShareBoardlistener;

import java.util.Map;

/**
 * Created by Jason on 2016/9/12.
 */
public class RecommendActivity extends BaseActivity implements View.OnClickListener
{
    private LinearLayout back;
    private TextView headTitle;
    private Button recommendBtn;
    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onResult(SHARE_MEDIA platform) {
            Log.d("plat","platform"+platform);
            Toast.makeText(RecommendActivity.this, platform + " 分享成功啦", Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(RecommendActivity.this,platform + " 分享失败啦", Toast.LENGTH_SHORT).show();
            if(t!=null){
                Log.d("throw","throw:"+t.getMessage());
            }
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(RecommendActivity.this,platform + " 分享取消了", Toast.LENGTH_SHORT).show();
        }
    };
    @Override
    protected void initData() {
        recommendBtn.setOnClickListener(this);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(Build.VERSION.SDK_INT>=23) {
            String[] mPermissionList = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CALL_PHONE, Manifest.permission.READ_LOGS, Manifest.permission.READ_PHONE_STATE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.SET_DEBUG_APP, Manifest.permission.SYSTEM_ALERT_WINDOW, Manifest.permission.GET_ACCOUNTS, Manifest.permission.WRITE_APN_SETTINGS};
            ActivityCompat.requestPermissions(this, mPermissionList, 123);
        }
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        back  = bindView(R.id.img_back);
        back.setVisibility(View.VISIBLE);
        back.setOnClickListener(this);
        headTitle = bindView(R.id.head_title);
        headTitle.setText("推荐有奖");
        headTitle.setVisibility(View.VISIBLE);
        recommendBtn = bindView(R.id.nowshare);

    }

    @Override
    protected int getId() {
        return R.layout.activity_recommend;
    }

    public static void RecommendIntent(Context context){
        Intent intent = new Intent(context,RecommendActivity.class);
        context.startActivity(intent);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.img_back:
                finish();
                break;
            case R.id.nowshare:
                   ShareMsg();
                break;
        }

    }
    private void ShareMsg(){
        new ShareAction(RecommendActivity.this)
                .setDisplayList(SHARE_MEDIA.WEIXIN,
                        SHARE_MEDIA.WEIXIN_CIRCLE)
                .setShareboardclickCallback(shareBoardlistener).open();
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(BaseApplication.getContext()).onActivityResult(requestCode, resultCode, data);
    }
    private ShareBoardlistener shareBoardlistener = new ShareBoardlistener() {
        @Override
        public void onclick(SnsPlatform snsPlatform, SHARE_MEDIA share_media) {
            if (share_media!=null){
                UMImage image = new UMImage(RecommendActivity.this,R.mipmap.app_icon);
                    new ShareAction(RecommendActivity.this).
                            setPlatform(share_media) .
                            withText("骑骑出行") .
                            withMedia(image).
                            withTitle("推荐有奖").
                            withTargetUrl("http://lin.sitayun.com:8090/linkboard/app/driverRegister_one.html?recommendDriver="+SpUtils.getString("AccoundID", null, BaseApplication.getContext())).
                            setCallback(umShareListener)
                            .share();
                }

            }

    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
